package com.cx.base.day13_io.io_01_file;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

/**
 * @author shkstart
 * @date 8/1/2019 - 11:00 AM
 */

public class Test_1_File_基本方法的使用 {
    public static void main(String[] args) throws IOException {
//        test2();
        test();

    }

    public static void test() {
//        File file1 = new File("f:\\A\\b");
//        file1.mkdir();
        Properties properties = System.getProperties();
        String[] strings = properties.toString().split(",");
        for (String str : strings) {
            System.out.println(str);
        }
        String separator = System.getProperty("file.separator");
        File file1 = new File("." + separator + "a1" + separator + "b");
        file1.mkdirs();
        File file2 = new File("d:\\A666\\b\\test.txt");
        try {
//            如果文件没有上层目录，要先获取直接到上层目录，然后mkdirs
            if (!file2.getParentFile().exists()) {
                file2.getParentFile().mkdirs();
            }
            file2.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            System.out.println("创建失败");
            e.printStackTrace();
        }
        System.out.println(file1.toPath());
        System.out.println(file1.getPath());
        System.out.println(file1.getAbsolutePath());
        System.out.println(file1.getParent());
        System.out.println(file1.getParentFile());
        System.out.println(file1.getParentFile().toString());
        System.out.println(file2.getParentFile().toString());
        System.out.println(file2.getAbsolutePath());
        System.out.println(file2.getPath());
        System.out.println();
    }

    public static void test2() throws IOException {

        File file1 = new File("n\\s\\b\\q\\tet.txt");
        try {
//        只能在已有路径下创建本地文件
            file1.createNewFile();
        } catch (Exception e) {
            System.out.println("跳过！");
        }
//        如果父目录没有，则自动创建父目录，如果父目录有的话用mkdir()
        file1.mkdirs();

        Properties properties = System.getProperties();
        String[] strings = properties.toString().split(",");

        for (String str : strings) {
            System.out.println(str);
        }

        File file2 = new File("f:/n/s/s");
        file2.mkdirs();
        File file3 = new File("f:/n/s/s/t.txt");
        file2.createNewFile();
        String dir = "f:/n/s";
        String[] nameList = file1.list(new FilenameFilter() {
                                           @Override
                                           public boolean accept(File dir, String name) {
                                               return name.endsWith(".java") || new File(name).isDirectory();
                                           }
                                       }
        );
        for (String name : nameList) {
            System.out.println(name);
        }
    }

    public static void test3() throws IOException {
        //数据
        byte[] data = {0, 2, 4, 6, 8, 10, 12, 14};
        //写入文件
        File file = new File("example.txt");
        OutputStream outputStream = new FileOutputStream(file);
        outputStream.write(data, 0, 8);
        outputStream.close();
        System.out.println("Exit.");
    }
}
